package com.hanwei.uap.acceptors.sstn.channel;

import com.hanwei.uap.common.enums.Status;
import com.hanwei.uap.common.util.JsonUtil;
import com.hanwei.uap.acceptors.api.channel.SessionManager;
import com.hanwei.uap.acceptors.api.constants.ChannelKey;
import com.hanwei.uap.acceptors.api.constants.Server;
import com.hanwei.uap.acceptors.api.utils.ChannelUtils;
import com.hanwei.uap.model.vo.SessionStatus;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class NettySessionManager implements SessionManager {
    private Logger log = LoggerFactory.getLogger("channel");
    @Autowired
    private Environment env;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value(value = "${uap.qname.sessionsyn}")
    private String sessionSynQname;
    private String serverHost = Server.DEFAULT_BIND_HOST;
    private int serverPort = Server.DEFAULT_BIND_PORT;

    @PostConstruct
    private void init() {
        if (env.getProperty("uap.server.host") != null) {
            serverHost = env.getProperty("uap.server.host");
        }
        if (env.getProperty("uap.server.port") != null) {
            serverPort = env.getProperty("uap.server.port", Integer.class);
        }
    }

    @Override
    public String onlineSyn(Channel channel) {
        String deviceId = ChannelUtils.getDeviceId(channel);
        Integer deviceType = ChannelUtils.getDeviceType(channel, ChannelKey.DEVICETYPE);
        SessionStatus status = new SessionStatus();
        status.setChannelId(channel.id().asShortText());
        status.setDeviceId(deviceId);
        status.setDeviceType(deviceType+"");
        status.setServerHost(serverHost);
        status.setServerPort(serverPort);
        status.setStatus(Status.ONLINE.getValue());
        String activeTime = ChannelUtils.getAttribute(channel, ChannelKey.TIME_ACTIVE);
        status.setUpdateTime(activeTime);
        status.setClientHost(ChannelUtils.getRemoteHost(channel));
        status.setClientPort(ChannelUtils.getRemotePort(channel));
        String json = JsonUtil.toJSON(status);
        BoundListOperations<String, String> list = stringRedisTemplate.boundListOps(sessionSynQname);
        list.leftPush(json);
        return json;
    }

    @Override
    public String offlineSyn(Channel channel) {
        String deviceId = ChannelUtils.getDeviceId(channel);
        Integer deviceType = ChannelUtils.getDeviceType(channel, ChannelKey.DEVICETYPE);
        SessionStatus status = new SessionStatus();
        status.setChannelId(channel.id().asShortText());
        status.setDeviceId(deviceId);
        status.setDeviceType(deviceType+"");
        status.setServerHost(serverHost);
        status.setServerPort(serverPort);
        status.setStatus(Status.OFFLINE.getValue());
        String inactiveTime = ChannelUtils.getAttribute(channel, ChannelKey.TIME_INACTIVE);
        status.setUpdateTime(inactiveTime);
        status.setClientHost(ChannelUtils.getRemoteHost(channel));
        status.setClientPort(ChannelUtils.getRemotePort(channel));
        String json = JsonUtil.toJSON(status);
        BoundListOperations<String, String> list = stringRedisTemplate.boundListOps(sessionSynQname);
        list.leftPush(json);
        return json;
    }
}
